package cn.jcet.mapreduce.compress;

import com.jcraft.jsch.IO;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.io.compress.CompressionCodec;
import org.apache.hadoop.io.compress.CompressionCodecFactory;
import org.apache.hadoop.io.compress.CompressionInputStream;
import org.apache.hadoop.io.compress.CompressionOutputStream;
import org.apache.hadoop.util.ReflectionUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

/**
 * @author 鲜磊
 * @Date 2020/12/18 23:03
 */
public class CompressTest {

    public static void main(String[] args) throws Exception {

       // compress("e:/code/hello.txt","org.apache.hadoop.io.compress.GzipCodec");
        compress("e:/code/hello.txt","org.apache.hadoop.io.compress.DefaultCodec");
  //      compress("e:/code/hello.txt","org.apache.hadoop.io.compress.GzipCodec");

        decompress("");
    }

    private static void decompress(String fileName) throws Exception {
        CompressionCodecFactory factory = new CompressionCodecFactory(new Configuration());
        CompressionCodec codec = factory.getCodec(new Path(fileName));

        if (codec == null){
            System.out.println("cannot find codec for file" + fileName);
            return;
        }

        // 输入流
        FileInputStream fis = new FileInputStream(new File(fileName));

        CompressionInputStream cis = codec.createInputStream(fis);

        FileOutputStream fos = new FileOutputStream(new File(fileName+".decoded"));

        IOUtils.copyBytes(cis,fos,1024*1024,false);

        fos.close();
        cis.close();
        fis.close();

    }

    private static void compress(String fileName, String method) {
        try {
            // 创建输入流
            FileInputStream fis = new FileInputStream(new File(fileName));
            // 获取输入流
            Class forName = Class.forName(method);
            CompressionCodec codec = (CompressionCodec)ReflectionUtils.newInstance(forName, new Configuration());
            // 拼接压缩格式
            FileOutputStream fos = new FileOutputStream(new File(fileName + codec.getDefaultExtension()));
            // 创建压缩输出流
            CompressionOutputStream cos = codec.createOutputStream(fos);
            //流的数据对拷
            IOUtils.copyBytes(fis,cos,1024*1024,false);
            // 关闭资源
            IOUtils.closeStream(cos);
            IOUtils.closeStream(fos);
            IOUtils.closeStream(fis);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
